using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.GPS;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;

namespace FTPPlugin.Settings
{
    public partial class SettingsPageControl : UserControl
    {
        public SettingsPageControl()
        {
            InitializeComponent();
            RefreshPage();
        }
        #region Public methods
        public void RefreshPage()
        {
            txtFTPUrl.Text = Plugin.FTPUserUrl;
            txtFTPFile.Text = Plugin.FTPUserFile;
            txtFTPUser.Text = Plugin.FTPUserName;
            txtFTPPassword.Text = Plugin.FTPUserPassword;
            chkSavePassword.Checked = Plugin.FTPUserSavePassword;
            if (chkSavePassword.Checked)
                txtFTPPassword.Enabled = true;
            else
                txtFTPPassword.Enabled = false;
            txtFTPAPIKey.Text = Plugin.FTPUserAPIKey;
            txtFTPPageTitle.Text = Plugin.FTPUserPageTitle;
            chkLocation.Checked = Plugin.FTPUserLocation;
            chkStartTime.Checked = Plugin.FTPUserStartTime;
            chkCategory.Checked = Plugin.FTPUserCategory;
            chkDistance.Checked = Plugin.FTPUserDistance;
            chkTotalTime.Checked = Plugin.FTPUserTotalTime;
            chkAvgSpeed.Checked = Plugin.FTPUserAvgSpeed;
            chkCalories.Checked = Plugin.FTPUserCalories;
            chkWeather.Checked = Plugin.FTPUserWeather;
            chkEquipment.Checked = Plugin.FTPUserEquipment;
            chkNotes.Checked = Plugin.FTPUserNotes;
            chkSplits.Checked = Plugin.FTPUserGraphSplits;
            txtSplitDistance.Text = Plugin.FTPUserSplitDistance;
            lblSplitDistance.Text = "Split Distance (" + Plugin.GetApplication().SystemPreferences.DistanceUnits.ToString()+")";

            lblURL.Text = Resources.Resources.FTP_LblDestinationFolder;
            lblURLHelp.Text = Resources.Resources.FTP_LblURLHelp;
            lblDestFile.Text = Resources.Resources.FTP_LblDestinationFile;
            lblDestFileHelp.Text = Resources.Resources.FTP_LblFileHelp;
            lblUser.Text = Resources.Resources.FTP_LblUser;
            lblPassword.Text = Resources.Resources.FTP_LblPassword;
            chkSavePassword.Text = Resources.Resources.FTP_LblSavePass;
            lblAPIKey.Text = Resources.Resources.FTP_LblAPIKey;
            lnkLblAPIHelp.Text = Resources.Resources.FTP_LblAPIHelp;
            LinkArea tmp = new System.Windows.Forms.LinkArea(lnkLblAPIHelp.Text.IndexOf("www."), 25);
            lnkLblAPIHelp.LinkArea = tmp;
//            lnkLblAPIHelp.LinkArea.Start = lnkLblAPIHelp.Text.IndexOf("www.");
            lblMapType.Text = Resources.Resources.FTP_LblMapType;
            lblTitle.Text = Resources.Resources.FTP_LblDefTitle;
            lblTitleHelp.Text = Resources.Resources.FTP_LblTitleHelp;
            lblTemplate.Text = Resources.Resources.FTP_LblTemplate;
            lblTemplateHelp.Text = Resources.Resources.FTP_LblTemplateHelp;
            lblIndex.Text = Resources.Resources.FTP_LblIndex;
            lblIndexHelp.Text = Resources.Resources.FTP_LblIndexHelp;
            lblDateFormat.Text = Resources.Resources.FTP_LblDateFormat;
            lblIncludeData.Text = Resources.Resources.FTP_LblIncludeData;
            chkLocation.Text = Resources.Resources.FTP_RptLocation;
            chkStartTime.Text = Resources.Resources.FTP_RptStartTime;
            chkCategory.Text = Resources.Resources.FTP_RptCategory;
            chkDistance.Text = Resources.Resources.FTP_LblDistance;
            chkTotalTime.Text = Resources.Resources.FTP_LblTime;
            chkAvgSpeed.Text = Resources.Resources.FTP_LblSpeed;
            chkCalories.Text = Resources.Resources.FTP_LblCalories;
            chkWeather.Text = Resources.Resources.FTP_RptWeather;
            chkEquipment.Text = Resources.Resources.FTP_RptEquipment;
            chkNotes.Text = Resources.Resources.FTP_RptNotes;
            lblSplitConfig.Text = Resources.Resources.FTP_LblSplitConfig;
            chkSplits.Text = Resources.Resources.FTP_LblSplits;
            lblSplitDistance.Text = Resources.Resources.FTP_LblSplitDist;
            lblSpecialConfig.Text = Resources.Resources.FTP_LblSpecialConfig;
            chkPassiveMode.Text = Resources.Resources.FTP_LblPassive;

        }

        public void ThemeChanged(ITheme visualTheme)
        {
            //txtActivateAtImport.ThemeChanged(visualTheme);
        }

        public void UICultureChanged(CultureInfo culture)
        {
            // Data contains localized text also, refresh.
            RefreshPage();
        }
        #endregion

        private void txtFTPUrl_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserUrl = txtFTPUrl.Text;
            if (!Plugin.FTPUserUrl.EndsWith("/"))
                Plugin.FTPUserUrl += "/";
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("www.google.com/apis/maps");
        }

        private void txtFTPFile_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserFile = txtFTPFile.Text;
        }

        private void txtFTPUser_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserName = txtFTPUser.Text;
        }

        private void txtFTPPassword_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserPassword = txtFTPPassword.Text;
        }

        private void chkSavePassword_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserSavePassword = chkSavePassword.Checked;
            txtFTPPassword.Text = "";
            RefreshPage();
        }

        private void txtFTPAPIKey_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserAPIKey = txtFTPAPIKey.Text;
        }

        private void txtFTPPageTitle_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserPageTitle = txtFTPPageTitle.Text;
        }

        private void chkLocation_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserLocation = chkLocation.Checked;
        }

        private void chkStartTime_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserStartTime = chkStartTime.Checked;
        }

        private void chkCategory_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserCategory = chkCategory.Checked;
        }

        private void chkDistance_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserDistance = chkDistance.Checked;
        }

        private void chkTotalTime_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserTotalTime = chkTotalTime.Checked;
        }

        private void chkAvgSpeed_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserAvgSpeed = chkAvgSpeed.Checked;
        }

        private void chkCalories_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserCalories = chkCalories.Checked;
        }

        private void chkWeather_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserWeather = chkWeather.Checked;
        }

        private void chkEquipment_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserEquipment = chkEquipment.Checked;
        }

        private void chkNotes_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserNotes = chkNotes.Checked;
        }

        private void SettingsPageControl_Load(object sender, EventArgs e)
        {
// Used to store files in plugin dll folder, had to move since using s2plugin method removes all files from directory
//            string FileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + Path.DirectorySeparatorChar;
//            string directoryString = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "STFTPPlugin" + Path.DirectorySeparatorChar;
            if (!Directory.Exists(Plugin.directoryString))
                Directory.CreateDirectory(Plugin.directoryString);

            cboTemplate.Items.Add("Generate New");
            cboIndexFile.Items.Add("None");
            DirectoryInfo myDir = new DirectoryInfo(Plugin.directoryString);
            foreach (FileInfo myFile in myDir.GetFiles("*.html"))
            {
                cboTemplate.Items.Add(myFile.Name);
                cboIndexFile.Items.Add(myFile.Name);
            }
            if (cboTemplate.Items.Contains(Plugin.FTPUserTemplate))
                cboTemplate.Text = Plugin.FTPUserTemplate;
            else
                cboTemplate.SelectedText = "Generate New";
            cboMapType.Items.Add("Street");
            cboMapType.Items.Add("Satellite");
            cboMapType.Items.Add("Hybrid");
            cboMapType.Items.Add("Terrain");
            if (Plugin.FTPUserMapType != null)
            {
                if (cboMapType.Items.Contains(Plugin.FTPUserMapType))
                    cboMapType.Text = Plugin.FTPUserMapType;
                else
                    cboMapType.Text = "Street";
            }
            else
                cboMapType.Text = "Street";
            cboDateFormat.Items.Add("YYYY-MM-DD");
            cboDateFormat.Items.Add("MM-DD-YYYY");
            cboDateFormat.Items.Add("DD-MM-YYYY");
            if (Plugin.FTPUserDateFormat != null)
            {
                if (cboDateFormat.Items.Contains(Plugin.FTPUserDateFormat))
                    cboDateFormat.Text = Plugin.FTPUserDateFormat;
                else
                    cboDateFormat.Text = "YYYY-MM-DD";
            }
            else
                cboDateFormat.Text = "YYYY-MM-DD";
            if (Plugin.FTPUserIndexFile != null)
            {
                if (cboIndexFile.Items.Contains(Plugin.FTPUserIndexFile))
                    cboIndexFile.Text = Plugin.FTPUserIndexFile;
                else
                    cboIndexFile.Text = "None";
            }
            else
                cboIndexFile.Text = "None";
        }

        private void cboTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserTemplate = cboTemplate.Text;
        }

        private void cboMapType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserMapType = cboMapType.Text;
        }

        private void cboIndexFile_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserIndexFile = cboIndexFile.Text;
        }

        private void chkGraphSplits_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserGraphSplits = chkSplits.Checked;
        }

        private void txtSplitDistance_TextChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserSplitDistance = txtSplitDistance.Text;
        }

        private void cboDateFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserDateFormat = cboDateFormat.Text;
        }

        private void chkPassiveMode_CheckedChanged(object sender, EventArgs e)
        {
            Plugin.FTPUserPassive = chkPassiveMode.Checked;
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Plugin.directoryString);
        }

    }
}
